package com.sdry.service.impl.cb;

import com.sdry.mapper.cb.DepreciationMapper;
import com.sdry.mapper.cb.ForkliEntityMapper;
import com.sdry.model.cb.DepreciationEntity;
import com.sdry.model.cb.ForkliEntity;
import com.sdry.service.cb.ForkliService;
import com.sdry.utils.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

@Service
public class ForkliServiceImpl implements ForkliService {

    @Autowired
    private ForkliEntityMapper forkliEntityMapper;

    @Autowired
    private DepreciationMapper depreciationMapper;

    @Override
    public List<ForkliEntity> getList(Long stockId, String date) {
        return forkliEntityMapper.findPageWithResult(stockId,date);
    }

    @Override
    public Integer add(ForkliEntity fe) throws ParseException {
        DateFormat format1 = new SimpleDateFormat("yyyy-MM-dd");
        //折旧
        if (StringUtil.isEmpty(fe.getBrand()) && StringUtil.isNotEmpty(fe.getParameter2())) {
            DepreciationEntity de = depreciationMapper.selectById(Long.parseLong(fe.getParameter2()));
            fe.setBrand(de.getItemName());
            List<DepreciationEntity> list = new ArrayList<>();
            list.add(de);
            BigDecimal bigDeMonery = DepreciationServiceImpl.getBigDeMonery(list,format1.parse(fe.getParameter3()));
            fe.setMonery(bigDeMonery);
            fe.setEquName(fe.getParameter5());
        }else {
            //租赁
            fe.setMonery(new BigDecimal(fe.getParameter()));
            fe.setEquName(fe.getParameter4());
        }
        fe.setCreatedate(format1.parse(fe.getParameter3()));
        return forkliEntityMapper.insertDynamic(fe);
    }

    @Override
    public Integer delete(String[] idArr) {
        Integer offectRow = 0;
        for (int i = 0; i < idArr.length; i++) {
            offectRow = forkliEntityMapper.setDelete(Long.parseLong(idArr[i]));
        }
        return offectRow;
    }
}
